home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / tscanf.c < prev    next >
C/C++ Source or Header  |  1992-03-28  |  3KB  |  93 lines

  1. /*
  2.  * scanf regression test
  3.  *
  4.  * usage: tscanf < tscanf.in
  5.  *
  6.  * please add to this
  7.  * 
  8.  * ++jrb    bammi@cadence.com
  9.  *  with parts from glibc
  10.  */
  11. #include <stdio.h>
  12. #if __STDC__
  13. #  include <stddef.h>
  14. #  ifndef EXIT_FAILURE
  15. #    include <stdlib.h>        /* HP strikes again! */
  16. #  endif
  17. #else
  18. #  define EXIT_FAILURE    (1)        /* failure return value for exit() */
  19. #  define EXIT_SUCCESS    (0)        /* success return value for exit() */
  20. #endif
  21.  
  22. int main()
  23. {
  24.     int i, n;
  25.     float x;
  26.     char name[50];
  27.     int exit_val = EXIT_SUCCESS;
  28.     
  29.     /* input: 25 54.32E-1 thompson */
  30.     /* output: n = 3 i = 25 x = 5.432000 name = :thompson: */
  31.     n = scanf("%d%f%s", &i, &x, name);
  32.     printf("Expect:\tn = 3 i = 25 x = 5.432000 name = :thompson:\n");
  33.     printf("Got:\tn = %d i = %d x = %f name = :%s:\n\n", n, i, x, name);
  34.     if(n != 3) exit_val |= EXIT_FAILURE;
  35.     
  36.     /* input:  56789 0123 56a72 */
  37.     /* output: n = 3 i = 56 x = 789.000000 name = :56: */
  38.     /* a72 left over */
  39.     n = scanf("%2d%f%*d %[0-9]", &i, &x, name);
  40.     printf("Expect:\tn = 3 i = 56 x = 789.000000 name = :56:\n");
  41.     printf("Got:\tn = %d i = %d x = %f name = :%s:\n\n", n, i, x, name);
  42.     if(n != 3) exit_val |= EXIT_FAILURE;
  43.  
  44.     /* output: n = 1 name = :a72: */
  45.     n = scanf("%s", name);
  46.     printf("Expect:\tn = 1 name = :a72:\n");
  47.     printf("Got:\tn = %d name = :%s:\n\n", n, name);
  48.     if(n != 1) exit_val |= EXIT_FAILURE;
  49.  
  50.     /* input:  56789 0123 56a72 */
  51.     /* output: n = 3 i = 56 x = 789.000000 name = :56: */
  52.     /* 'a72' left over */
  53.     n = scanf("%2d%f%*d %[0123456789]", &i, &x, name);
  54.     printf("Expect:\tn = 3 i = 56 x = 789.000000 name = :56:\n");
  55.     printf("Got:\tn = %d i = %d x = %f name = :%s:\n\n", n, i, x, name);
  56.     if(n != 3) exit_val |= EXIT_FAILURE;
  57.  
  58.     /* output: n = 1 name = :a72: */
  59.     n = scanf("%s", name);
  60.     printf("Expect:\tn = 1 name = :a72:\n");
  61.     printf("Got:\tn = %d name = :%s:\n\n", n, name);
  62.     if(n != 1) exit_val |= EXIT_FAILURE;
  63.  
  64.     /* input: 2 quarts of oil
  65.           10.0LBS      of       fertilizer
  66.           100ergs of energy
  67.        output:
  68.               count = 3, quant = 2.000000, item = oil, units = quarts
  69.           count = 3, quant = 10.000000, item = fertilizer, units = LBS
  70.           count = 3, quant = 100.000000, item = energy, units = rgs
  71.           count = -1, quant = 0.000000, item = , units = 
  72.     */
  73.     printf("Expect:\n\tcount = 3, quant = 2.000000, item = oil, units = quarts\n"); 
  74.     printf("\tcount = 3, quant = 10.000000, item = fertilizer, units = LBS\n");
  75.     printf("\tcount = 3, quant = 100.000000, item = energy, units = rgs\n");
  76.     printf("\tcount = -1, quant = 0.000000, item = , units = \n");
  77.     printf("Got:\n");
  78.     while (!feof (stdin) && !ferror (stdin))
  79.     {
  80.     float quant = 0.0;
  81.     char units[21], item[21];
  82.     units[0] = item[0] = '\0';
  83.     n = fscanf (stdin, "%f%20s of %20s", &quant, units, item);
  84.     (void) fscanf (stdin, "%*[^\n]");
  85.     printf ("\tcount = %d, quant = %f, item = %.21s, units = %.21s\n",
  86.          n, quant, item, units);
  87.     }
  88.     if(n != EOF) exit_val |= EXIT_FAILURE;
  89.    
  90.     return exit_val;
  91. }
  92.  
  93.